package dao

import "gosup/models"

func GetAllPurchaseMain(purchaseMainParam map[string]interface{}) (error, []models.PurchaseMain, int64) {
	var purchaseMainData []models.PurchaseMain
	page := purchaseMainParam["page"].(int)
	pageSize := purchaseMainParam["limit"].(int)
	searchName := purchaseMainParam["searchName"].(string)
	var total int64
	err := db.Table("purchase_main p").Select("p.purchase_list_id, p.purchase_list_number, p.purchase_list_price, p.purchase_list_remarks, p.purchase_list_staff, s.staff_name purchase_list_staff_name, p.purchase_list_time, p.update_time, p.is_del").Joins("INNER JOIN erp_staff s ON p.purchase_list_staff = s.staff_id").Where("s.staff_name like ? and p.is_del = false", searchName+"%").Order("purchase_list_id ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&purchaseMainData).Error
	return err, purchaseMainData, total
}

func DelPurchaseMain(id int) error {
	err := db.Table("purchase_main").Where("purchase_list_id = ?", id).Update("is_del", true).Error
	return err
}

func UpdatePurchaseMain(purchaseMain models.PurchaseMain) error {
	err := db.Table("purchase_main").Select("purchase_list_staff",  "purchase_list_remarks", "update_time", "is_del").Where("purchase_list_id = ?", purchaseMain.PurchaseMainId).Updates(&purchaseMain).Error
	return err
}

func AddPurchaseMain(purchaseList models.PurchaseMain) (error) {
	err := db.Table("purchase_main").Select("purchase_main_staff", "purchase_list_number", "purchase_list_price", "purchase_list_time", "purchase_list_remarks", "update_time", "is_del").Create(&purchaseList).Error
	return err
}
